package model.data.container;

/**
 * ProductContainer.java
 * Purpose: A container to hold product objects.
 * 
 * @author Hans Brems
 * @version 0.1 12/01/2012
 */

import java.util.ArrayList;
import java.util.List;

import model.Product;
import model.data.DuplicateRecordException;
import model.data.ProductStore;
import model.data.RecordNotFoundException;

public class ProductContainer implements ProductStore {
	/* List of products */
	private ArrayList<Product> products = new ArrayList<Product>();
	
	/** Writes a new product */
	@Override
	public void write(Product product) throws DuplicateRecordException {
		if (this.getById(product.getId()) == null) {
			this.products.add(product);
		}
		else
		{
			throw new DuplicateRecordException();
		}
	}

	/** Updates a product */
	@Override
	public void update (Product product) {
		// TODO update logic
		// currently impossible due to referencing issue
	}

	/** Deletes a product */
	@Override
	public void delete(Product product) throws RecordNotFoundException {
		if (this.getById(product.getId()) != null) {
			this.products.remove(product);
		}
		else
		{
			throw new RecordNotFoundException();
		}
	}

	/** Returns the number of products in the data store */
	@Override
	public int getCount() {
		return this.products.size();
	}

	/** Returns a product from the container with a specific id */
	@Override
	public List<Product> getById(long id) {
		ArrayList<Product> products = new ArrayList<Product>();
		for (Product product : this.products){
			if(product.getId() == id){
				products.add(product);
			}
		}
		return products;
	}
}
